package com.wgke.bluetooth.utils;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class BluetoothServiceNameResolver {

    // 标准服务UUID与名称的映射表
    private static final Map<UUID, String> STANDARD_SERVICE_NAMES = new HashMap<UUID, String>() {{
        // 通用服务
        put(UUID.fromString("00001800-0000-1000-8000-00805f9b34fb"), "Generic Access");
        put(UUID.fromString("00001801-0000-1000-8000-00805f9b34fb"), "Generic Attribute");

        // 常用服务
        put(UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb"), "Device Information");
        put(UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb"), "Heart Rate");
        put(UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb"), "Battery Service");
        put(UUID.fromString("00001810-0000-1000-8000-00805f9b34fb"), "Blood Pressure");
        put(UUID.fromString("00001811-0000-1000-8000-00805f9b34fb"), "Alert Notification Service");
        put(UUID.fromString("00001812-0000-1000-8000-00805f9b34fb"), "Human Interface Device");
        put(UUID.fromString("00001813-0000-1000-8000-00805f9b34fb"), "Scan Parameters");
        put(UUID.fromString("00001814-0000-1000-8000-00805f9b34fb"), "Running Speed and Cadence");
        put(UUID.fromString("00001816-0000-1000-8000-00805f9b34fb"), "Cycling Speed and Cadence");

        // 其他服务
        put(UUID.fromString("00001802-0000-1000-8000-00805f9b34fb"), "Immediate Alert");
        put(UUID.fromString("00001803-0000-1000-8000-00805f9b34fb"), "Link Loss");
        put(UUID.fromString("00001804-0000-1000-8000-00805f9b34fb"), "Tx Power");
        put(UUID.fromString("00001805-0000-1000-8000-00805f9b34fb"), "Current Time Service");
        put(UUID.fromString("00001806-0000-1000-8000-00805f9b34fb"), "Reference Time Update Service");
        put(UUID.fromString("00001807-0000-1000-8000-00805f9b34fb"), "Next DST Change Service");
        put(UUID.fromString("00001808-0000-1000-8000-00805f9b34fb"), "Glucose");
        put(UUID.fromString("00001809-0000-1000-8000-00805f9b34fb"), "Health Thermometer");
        put(UUID.fromString("0000180e-0000-1000-8000-00805f9b34fb"), "Phone Alert Status Service");
        put(UUID.fromString("00001815-0000-1000-8000-00805f9b34fb"), "Automation IO");
        put(UUID.fromString("00001817-0000-1000-8000-00805f9b34fb"), "Cycling Power");
        put(UUID.fromString("00001818-0000-1000-8000-00805f9b34fb"), "Location and Navigation");
        put(UUID.fromString("00001819-0000-1000-8000-00805f9b34fb"), "Environmental Sensing");
        put(UUID.fromString("0000181a-0000-1000-8000-00805f9b34fb"), "Body Composition");
        put(UUID.fromString("0000181b-0000-1000-8000-00805f9b34fb"), "User Data");
        put(UUID.fromString("0000181c-0000-1000-8000-00805f9b34fb"), "Weight Scale");
        put(UUID.fromString("0000181d-0000-1000-8000-00805f9b34fb"), "Bond Management Service");
        put(UUID.fromString("0000181e-0000-1000-8000-00805f9b34fb"), "Continuous Glucose Monitoring");
        put(UUID.fromString("0000181f-0000-1000-8000-00805f9b34fb"), "Internet Protocol Support Service");
        put(UUID.fromString("00001820-0000-1000-8000-00805f9b34fb"), "Indoor Positioning");
        put(UUID.fromString("00001821-0000-1000-8000-00805f9b34fb"), "Pulse Oximeter Service");
        put(UUID.fromString("00001822-0000-1000-8000-00805f9b34fb"), "HTTP Proxy");
        put(UUID.fromString("00001823-0000-1000-8000-00805f9b34fb"), "Transport Discovery");
        put(UUID.fromString("00001824-0000-1000-8000-00805f9b34fb"), "Object Transfer Service");
        put(UUID.fromString("00001825-0000-1000-8000-00805f9b34fb"), "Fitness Machine");
        put(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb"), "Mesh Provisioning Service");
        put(UUID.fromString("00001827-0000-1000-8000-00805f9b34fb"), "Mesh Proxy Service");
        put(UUID.fromString("00001828-0000-1000-8000-00805f9b34fb"), "Reconnection Configuration");
        put(UUID.fromString("00001829-0000-1000-8000-00805f9b34fb"), "Insulin Delivery");
    }};

    /**
     * 获取标准服务的名称
     *
     * @param serviceUuid
     * @return 服务名称，如果是标准服务返回对应的名称，否则返回"Unknown Service"或UUID字符串
     */
    public static String getServiceName(UUID serviceUuid) {
        if (serviceUuid == null) {
            return "Null Service";
        }

        String serviceName = STANDARD_SERVICE_NAMES.get(serviceUuid);

        if (serviceName != null) {
            return serviceName;
        } else {
            // 对于非标准服务，可以返回UUID字符串或"Unknown Service"
            return "Unknown Service (自定义厂商服务)";
        }
    }

    /**
     * 检查是否是标准服务
     *
     * @param serviceUuid 服务UUID
     * @return 如果是标准服务返回true，否则返回false
     */
    public static boolean isStandardService(UUID serviceUuid) {
        return STANDARD_SERVICE_NAMES.containsKey(serviceUuid);
    }


    /***
     *
     * BluetoothGattCharacteristic 下的uuid
     * */
    public static String getCharacteristicName(UUID uuid) {
        // 检查是否是标准UUID
        if (uuid.toString().startsWith("0000") && uuid.toString().endsWith("-0000-1000-8000-00805f9b34fb")) {
            String shortUuid = uuid.toString().substring(4, 8).toLowerCase();

            // 使用HashMap存储UUID到名称的映射
            Map<String, String> uuidToName = new HashMap<String, String>() {{
                // GAP 特征
                put("2a00", "Device Name");
                put("2a01", "Appearance");
                put("2a02", "Peripheral Privacy Flag");
                put("2a03", "Reconnection Address");
                put("2a04", "Peripheral Preferred Connection Parameters");

                // GATT 特征
                put("2a05", "Service Changed");

                // 设备信息
                put("2a24", "Model Number String");
                put("2a25", "Serial Number String");
                put("2a26", "Firmware Revision String");
                put("2a27", "Hardware Revision String");
                put("2a28", "Software Revision String");
                put("2a29", "Manufacturer Name String");
                put("2a2a", "IEEE 11073-20601 Regulatory Certification");
                put("2a50", "PnP ID");

                // 电池服务
                put("2a19", "Battery Level");
                put("2a1a", "Battery Power State");
                put("2a1b", "Battery Level State");
                put("2a1c", "Battery Time to Discharge");
                put("2a1d", "Battery Time to Charge");

                // 健康设备
                put("2a2b", "Heart Rate Measurement");
                put("2a2c", "Body Sensor Location");
                put("2a2d", "Heart Rate Control Point");
                put("2a34", "Glucose Measurement");
                put("2a35", "Glucose Measurement Context");
                put("2a36", "Glucose Feature");
                put("2a37", "Record Access Control Point");
                put("2a38", "RSC Measurement");
                put("2a39", "RSC Feature");
                put("2a3a", "SC Control Point");

                // 环境传感器
                put("2a6e", "Temperature");
                put("2a6f", "Humidity");
                put("2a1f", "Barometric Pressure");
                put("2a20", "Altitude");
                put("2a21", "Concentration");
                put("2a22", "Light Intensity");
                put("2a23", "Rainfall");

                // 添加更多标准UUID映射...
            }};

            return uuidToName.getOrDefault(shortUuid, "自定义特征");
        }
        return "Custom Characteristic";
    }

    /**
     * 读取(READ)相关属性：
     * PROPERTY_READ：允许读取特征值
     * PROPERTY_NOTIFY：允许通过通知接收特征值更新
     * PROPERTY_INDICATE：允许通过指示接收特征值更新（需要确认）
     * 写入(WRITE)相关属性：
     * PROPERTY_WRITE：允许写入特征值（需要响应）
     * PROPERTY_WRITE_NO_RESPONSE：允许写入特征值（不需要响应）
     * PROPERTY_SIGNED_WRITE：允许带签名的写入（需要安全连接）
     * 其他属性：
     * PROPERTY_BROADCAST：允许广播特征值
     * PROPERTY_EXTENDED_PROPS：表示特征有扩展属性
     */
    public static String getPropertyDescription(int properties) {
        List<String> propList = new ArrayList<>();
        if ((properties & BluetoothGattCharacteristic.PROPERTY_BROADCAST) != 0)
            propList.add("BROADCAST");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) != 0) propList.add("READ");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0) {
            propList.add("WRITE_NO_RESPONSE");
        }
        if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) propList.add("WRITE");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) propList.add("NOTIFY");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0)
            propList.add("INDICATE");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0)
            propList.add("SIGNED_WRITE");
        if ((properties & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) != 0)
            propList.add("EXTENDED_PROPS");
        return TextUtils.join(" | ", propList);
    }


}